package com.ruoyi.wms.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.wms.domain.bo.employee.PMetricDetailsBo;
import com.ruoyi.wms.domain.entity.employee.EmployeeSalary;
import com.ruoyi.wms.domain.entity.employee.PMetricDetails;
import com.ruoyi.wms.domain.entity.employee.PSalarySetup;
import com.ruoyi.wms.domain.vo.PMetricDetailsVo;
import com.ruoyi.wms.mapper.EmployeeSalaryMapper;
import com.ruoyi.wms.mapper.PMetricDetailsMapper;
import com.ruoyi.wms.mapper.PSalarySetupMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 绩效记录Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-12-01
 */
@RequiredArgsConstructor
@Service
public class PMetricDetailsService {

    private final PMetricDetailsMapper pMetricDetailsMapper;

    private final PSalarySetupMapper pSalarySetupMapper;
    private final EmployeeSalaryMapper employeeSalaryMapper;

    /**
     * 查询绩效记录
     */
    public PMetricDetailsVo queryById(Long id) {
        return pMetricDetailsMapper.selectVoById(id);
    }

    /**
     * 查询绩效记录列表
     */
    public TableDataInfo<PMetricDetailsVo> queryPageList(PMetricDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PMetricDetails> lqw = buildQueryWrapper(bo);
        Page<PMetricDetailsVo> result = pMetricDetailsMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询绩效记录列表
     */
    public List<PMetricDetailsVo> queryList(PMetricDetailsBo bo) {
        LambdaQueryWrapper<PMetricDetails> lqw = buildQueryWrapper(bo);
        return pMetricDetailsMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PMetricDetails> buildQueryWrapper(PMetricDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PMetricDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEmployeeId() != null, PMetricDetails::getEmployeeId, bo.getEmployeeId());
        lqw.eq(bo.getLevel() != null, PMetricDetails::getLevel, bo.getLevel());
        lqw.between(params.get("beginCalculationPeriod") != null && params.get("endCalculationPeriod") != null,
            PMetricDetails::getCalculationPeriod, params.get("beginCalculationPeriod"), params.get("endCalculationPeriod"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            PMetricDetails::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        return lqw;
    }

    /**
     * 新增绩效记录
     */
    public void insertByBo(PMetricDetailsBo bo) {
        //如果积分为空默认为100
        if (bo.getPoints() == null) {
            bo.setPoints(new BigDecimal(100));
        }
        //计算p项的工资
        BigDecimal pSalay = computePSalay(bo);
        //插入绩效记录
        bo.setPSalay(pSalay);
        PMetricDetails add = MapstructUtils.convert(bo, PMetricDetails.class);
        pMetricDetailsMapper.insert(add);

    }

    private BigDecimal computePSalay(PMetricDetailsBo bo) {
        //计算p项薪资
        PSalarySetup pSalarySetup = pSalarySetupMapper.selectOne(Wrappers.<PSalarySetup>lambdaQuery()
            .eq(PSalarySetup::getLevel, bo.getLevel()));
        if (ObjectUtil.isEmpty(pSalarySetup)) {
            throw new RuntimeException("该等级没有设置薪资");
        }
        ;
        //获取月初月末
        DateTime begin = DateUtil.beginOfMonth(bo.getCalculationPeriod());
        DateTime end = DateUtil.endOfMonth(bo.getCalculationPeriod());
        BigDecimal pSalay = pSalarySetup.getBaseSalary().multiply(bo.getPoints()).divide(new BigDecimal(100));
        //插入员工
        int update = employeeSalaryMapper.update(Wrappers.<EmployeeSalary>lambdaUpdate()
            .eq(bo.getEmployeeId() != null, EmployeeSalary::getEmployeeId, bo.getEmployeeId())
            .between(EmployeeSalary::getPayPeriod, begin, end)
            .set(EmployeeSalary::getAllowance, pSalay));
        if (update == 0) {
            throw new RuntimeException("该员工没有薪资记录");
        }
        return pSalay;
    }

    /**
     * 修改绩效记录
     */
    public void updateByBo(PMetricDetailsBo bo) {
        PMetricDetails update = MapstructUtils.convert(bo, PMetricDetails.class);
        BigDecimal pSalay = computePSalay(bo);
        update.setPSalay(pSalay);
        pMetricDetailsMapper.updateById(update);
    }

    /**
     * 批量删除绩效记录
     */
    public void deleteByIds(Collection<Long> ids) {
        pMetricDetailsMapper.deleteBatchIds(ids);
    }
}
